package com.rafahead.bloquinho;


import android.app.Activity;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.rafahead.bloquinho.dao.PlayerDAO;



public class PlayerListFragment extends ListFragment {

	public interface PlayerSelectedCallBack{
		public void onPlayerSelected(long id);
	}

	private static final int DELETE_ID = Menu.FIRST;

	private PlayerDAO mPlayerDAO;

	private PlayerSelectedCallBack mCallBack;

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		// Notify the parent activity of selected item
		mCallBack.onPlayerSelected(id);

		// Set the item as checked to be highlighted when in two-pane layout
		getListView().setItemChecked(position, true);

	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		// This makes sure that the container activity has implemented
		// the callback interface. If not, it throws an exception.
		try {
			mCallBack = (PlayerSelectedCallBack) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()+ " must implement PlayerSelectedCallBack");
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		/*        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
            setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
        }*/
		registerForContextMenu(getListView());
	}

	@Override
	public void onStart() {
		super.onStart();
		// When in two-pane layout, set the listview to highlight the selected list item
		// (We do this during onStart because at the point the listview is available.)
		if (getFragmentManager().findFragmentById(R.id.matchdetail_fragment) != null) {
			getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		}		
	}

	@Override
	public void onPause() {
		super.onPause();
		mPlayerDAO.close();
	}

	@Override
	public void onResume() {
		super.onResume();
		this.populate();
	}


	private void populate() {
		// We need to use a different list item layout for devices older than Honeycomb
		int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1 :android.R.layout.simple_list_item_1;

		mPlayerDAO = new PlayerDAO(getActivity());
		mPlayerDAO.open();

		Cursor cursor = mPlayerDAO.searchAllPlayers();
		getActivity().startManagingCursor(cursor);

		SimpleCursorAdapter spAdapter = new SimpleCursorAdapter(getActivity(), layout, cursor,new String[] { PlayerDAO.COLUMN_NAME},new int[] { android.R.id.text1});

		setListAdapter(spAdapter);
		
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.player_list_menu, menu);
		super.onCreateOptionsMenu(menu,inflater);		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_add_player:
			PlayerEditFragment newFragment = new PlayerEditFragment();
			android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.container, newFragment);
			transaction.addToBackStack(PlayerEditFragment.class.toString());
			transaction.commit();

			break;

		default:
			break;

		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case DELETE_ID:
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			
			
			mPlayerDAO = new PlayerDAO(getActivity());
			mPlayerDAO.open();
			
			mPlayerDAO.removePlayer(info.id);
			this.populate();
			return true;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		
		menu.add(0, DELETE_ID, 0, R.string.menu_delete);
		super.onCreateContextMenu(menu, v, menuInfo);
		
	}

}
